package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    static {
        Teacher teacher1 = new Teacher("heima001", "张三", "23", "1999-11-11");
        Teacher teacher2 = new Teacher("heima002", "李四", "24", "2000-11-11");

        teachers[0] = teacher1;
        teachers[1] = teacher2;
    }

    public static boolean addTeacher(Teacher t) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            teachers[index] = t;
            return true;
        }
    }

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public void deleteTeacherById(String deId) {
        int index = getIndex(deId);
        teachers[index] = null;
    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teachers[i].getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String updateId, Teacher newTeacher) {
        int index = getIndex(updateId);
        teachers[index] = newTeacher;
    }
}
